package model;

import java.io.File;

import javax.servlet.ServletConfig;
import javax.servlet.ServletException;

import model.PaymentDAO;
import model.PositionDAO;
import model.TransactionDAO;

import org.mybeans.factory.BeanTable;

/*
 * 
 * made by alesmana@andrew.cmu.edu
 * class for 15-600 homework 9
 * 
 * Model class
 * 
 */

public class Model {

    private EmployeeDAO employeeDAO;
    private CustomerDAO customerDAO;
    private FundDAO fundDAO;
    private FundHistoryDAO fundHistoryDAO;
    private FundHistoryDAOSQL fundHistoryDAOSQL;
    private TransactionDAO transactionDAO;
    private TransactionDAOSQL transactionDAOSQL;
    private PositionDAO positionDAO; 
    private PaymentDAO paymentDAO; 
    private PaymentDAOSQL paymentDAOSQL; 

    private String localNetAddr;
    private String localNetPrefix;
    private String jdbcDriver;
    private String jdbcURL;
    private String jdbcUser;
    private String jdbcPass;
    private boolean requireSSL;

    public Model(ServletConfig config) throws ServletException {

        localNetAddr = config.getInitParameter("localNetAddr");
        localNetPrefix = config.getInitParameter("localNetPrefix");
        requireSSL = new Boolean(config.getInitParameter("requireSSL"));

        jdbcDriver = config.getInitParameter("jdbcDriverName");
        jdbcURL = config.getInitParameter("jdbcURL");
        jdbcUser = "demo";
        jdbcPass = "";
        //userDAO = new UserDAO(jdbcDriver,jdbcURL);
        //bookmarkDAO = new BookmarkDAO(jdbcDriver,jdbcURL);

        try {
            //for comma separated value
            String csvDirStr = config.getInitParameter("csvDir");
            if (csvDirStr != null && csvDirStr.length() > 0) {
                File csvDir = new File(csvDirStr);
                BeanTable.useCSVFiles(csvDir);
            } else {
                String jdbcDriver = config.getInitParameter("jdbcDriverName");
                String jdbcURL = config.getInitParameter("jdbcURL");
                String jdbcUser = "demo";
                String jdbcPass = "";
                BeanTable.useJDBC(jdbcDriver, jdbcURL, jdbcUser, jdbcPass);
            }

            //userDAO = new UserDAO();
            fundDAO = new FundDAO();
            customerDAO = new CustomerDAO();
            employeeDAO = new EmployeeDAO();
            transactionDAO = new TransactionDAO();
            transactionDAOSQL = new TransactionDAOSQL(jdbcDriver,jdbcURL,jdbcUser, jdbcPass);
            fundHistoryDAO = new FundHistoryDAO();
            fundHistoryDAOSQL = new FundHistoryDAOSQL(jdbcDriver,jdbcURL,jdbcUser, jdbcPass);
            positionDAO = new PositionDAO();
            paymentDAO = new PaymentDAO();
            paymentDAOSQL = new PaymentDAOSQL(jdbcDriver,jdbcURL,jdbcUser, jdbcPass);
           // fundHistoryDAO = new FundHistoryDAOSQL(); 
            
        } catch (Exception e) {
            throw new ServletException(e);
        }
    }

    //put getter of DAO here

    public EmployeeDAO getEmployeeDAO(){
        return employeeDAO;
    }
    
    public CustomerDAO getCustomerDAO(){
        return customerDAO;
    }
    
    public FundDAO getFundDAO(){
        return fundDAO;
    }
    
    public FundHistoryDAO getFundHistoryDAO(){
        return fundHistoryDAO;
    }
    
    public FundHistoryDAOSQL getFundHistoryDAOSQL(){
        return fundHistoryDAOSQL;
    }
    
    public TransactionDAO getTransactionDAO() {
        return transactionDAO;
    }
    
    public TransactionDAOSQL getTransactionDAOSQL() {
        return transactionDAOSQL;
    }
    
    public PositionDAO getPositoinDAO(){
        return positionDAO;
    }
    
    public PaymentDAO getPaymentDAO(){
        return paymentDAO;
    }
    
    public PaymentDAOSQL getPaymentDAOSQL(){
        return paymentDAOSQL;
    }
    
    public boolean getRequireSSL() {
        return requireSSL;
    }
}
